package v1

import (
	"auth/bll"
	"auth/model"
	"auth/server/web/middleware"
	"auth/utils"
	"github.com/gin-gonic/gin"
)

var Authority = &authority{}

type authority struct{}

func (a *authority) Init(r *gin.RouterGroup) {
	g := r.Group("/authority", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
	}
}

// 创建角色
func (a *authority) create(c *gin.Context) {
	var (
		in  = &model.AuthorityCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Authority.Create(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *authority) update(c *gin.Context) {
	var (
		in  = &model.AuthorityUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Authority.Update(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *authority) list(c *gin.Context) {
	var (
		in  = &model.AuthorityListRequest{}
		out = &model.AuthorityListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.Authority.List(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}

func (a *authority) delete(c *gin.Context) {
	var (
		in  = &model.AuthorityDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Authority.Delete(in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}
